package org.taohuawu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.taohuawu.pojo.Item;
import org.taohuawu.service.ItemService;
import org.taohuawu.web.util.JsonResult;

import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Value("${server.port}")
    private int port;

    // http://localhost:8001/item/35
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        log.info("port=" + port + ",orderid=" + orderId);

        if (Math.random() < 0.6) {
            long t = new Random().nextInt(5000);
            log.info("暂停=" + t);
            Thread.sleep(t);
        }
        List<Item> items = itemService.getItems(orderId);
        return JsonResult.ok(items).msg("port=" + port);
    }

    // http://localhost:8001/item/decreaseNumber
    @PostMapping("/decreaseNumber")
    public JsonResult decreaseNumber(@RequestBody List<Item> list) {
        itemService.decreaseNumbers(list);
        return JsonResult.ok();
    }
}
